import configparser
import os.path
import sys


class Config:
    def __init__(self):
        # 读取文件
        self.base_dir = os.path.dirname(os.path.realpath(__file__))
        self.config_path = os.path.join(self.base_dir, "config.ini")
        self.config = configparser.ConfigParser()
        self.config.read(self.config_path)
        self.verify_config()

    def get_app_info(self):
        return {
            "app_version_name": self.config.get("appInfo", "app_version_name"),
            "app_version_code": self.config.get("appInfo", "app_version_code"),
            "apk_download_path": self.config.get("appInfo", "apk_download_path")
        }

    def get_lib_info(self):
        return {
            "build_tools_path": self.config.get("lib", "build_tools_path"),
            "java_home": self.config.get("lib", "java_home")
        }

    def get_channel_info(self):
        return {
            "prefix": self.config.get("channelInfo", "prefix"),
            "channel": self.config.get("channelInfo", "channel")
        }

    def get_sign_info(self):
        return {
            "storeFile": self.config.get("signInfo", "storeFile"),
            "keyAlias": self.config.get("signInfo", "keyAlias"),
            "keyPassword": self.config.get("signInfo", "keyPassword"),
            "storePassword": self.config.get("signInfo", "storePassword")
        }

    def get_legu_info(self):
        return {
            "ak": self.config.get("leguInfo", "ak"),
            "sk": self.config.get("leguInfo", "sk")
        }

    def verify_config(self):
        app_info = self.get_app_info()
        lib_info = self.get_lib_info()
        channel_info = self.get_channel_info()
        sign_info = self.get_sign_info()
        legu_info = self.get_legu_info()
        self.__is_empty(app_info)
        self.__is_empty(lib_info)
        self.__is_empty(channel_info)
        self.__is_empty(sign_info)
        self.__is_empty(legu_info)

    def __is_empty(self, data: dict):
        for k, v in data.items():
            if not v:
                print(f"配置项 {k} 不能为空")
                sys.exit(1)
